#!/usr/bin/env zsh
# Description:
#   Show the report of zplug errors

if [[ ! -f $_zplug_log[trace] ]] || [[ ! -s $_zplug_log[trace] ]]; then
    __zplug::io::print::f \
        --die \
        --zplug \
        --error \
        "The trace log is not found or empty\n"
    return 1
fi

local arg

while (( $# > 0 ))
do
    arg="$1"
    case "$arg" in
        edit)
            ${=${2:+$2}:-${EDITOR:-vim}} "$_zplug_log[trace]"
            return $status
            ;;
        jq)
            if (( ! $+commands[jq] )); then
                return 1
            fi
            cat "$_zplug_log[trace]" \
                | sed -e 's/[[:cntrl:]]//g' \
                | jq ${=${2:+"$argv[2,-1]"}:-"."}
            return $status
            ;;
        less | more)
            # $PAGER
            cat "$_zplug_log[trace]" | ${=arg}
            return $status
            ;;
        clear)
            rm -i "$_zplug_log[trace]"
            return $status
            ;;
        count)
            cat "$_zplug_log[trace]" | grep -c ''
            return $status
            ;;
        latest)
            cat "$_zplug_log[trace]" \
                | tail -n ${${2:+"$argv[2,-1]"}:-1} \
                | ${=${${commands[jq]:+"jq ."}:-"cat -"}}
            return $status
            ;;
        "")
            ;;
        *)
            __zplug::io::print::f \
                --die \
                --zplug \
                --func \
                "$arg: invalid arguments\n"
            return 1
            ;;
    esac
    shift
done

cat "$_zplug_log[trace]"
